const getCollection = (user_id) => {
    const formdata = new FormData();
    formdata.append('user_id', user_id);

    return dispatch => {
        let url = 'http://www.h5weixin.club:8000/collection'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            body: formdata
        }).then(res => {
            return res.json()
        }).then(res => {
            if (res.code === 200) {
                dispatch({
                    type: 'GET_COLLECT_DATA',
                    data: res.data
                })
            }
        }).catch(err => {
            console.log(err)
        })
    }
}

const addCollection = (user_id, animation_id, animation) => {
    const formdata = new FormData();
    formdata.append('user_id', user_id);
    formdata.append('animation_id', animation_id);
    console.log(formdata)
    return dispatch => {
        let url = 'http://www.h5weixin.club:8000/collection_info'
        fetch(url, {
            method: 'POST',
            mode: 'cors',
            body: formdata
        }).then(res => {
            return res.json()
        }).then(res => {
            console.log('in add collection')
            if (res.code === 200) {
                dispatch({
                    type: 'ADD_COLLECT_DATA',
                    msg: res.msg,
                    value: animation,
                    index: animation_id
                })
            }
        }).catch(err => {
            console.log(err)
        })
    }
}

// 根据用户和动漫id去删除收藏
// const delCollection = (user_id, collection_animation_id) => {
//     const formdata = new FormData();
//     formdata.append('user_id', user_id);
//     formdata.append('collection_animation_id', collection_animation_id);
//     console.log(formdata)

//     return dispatch => {

//         let url = 'http://www.h5weixin.club:8000/del_collection';
//         fetch(url, {
//             method: 'POST',
//             mode: 'cors',
//             body: formdata
//         }).then((res) => {
//             console.log('in')
//             return res.json()
//         }).then((res) => {
//             console.log('indel')
//             console.log(res)
//             if (res.code == 200) {
//                 console.log(res.data)
//                 dispatch({
//                     type: 'DEL_COLLECT_DATA',
//                     value: res.data,
//                     index: collection_animation_id
//                 })
//             }
//         }).catch((err) => {
//             console.log(err);
//         })
//     }
// }
const delCollection = (user_id, collection_animation_id) => {
    console.log(user_id, collection_animation_id)
    const formdata = new FormData();
    formdata.append('user_id', user_id)
    formdata.append('collection_animation_id', collection_animation_id)
    console.log(formdata);
    return dispatch => {
        fetch('http://www.h5weixin.club:8000/del_collection', {
            method: 'POST',
            mode: 'cors',
            body: formdata
        }).then(res => {
            console.log(res);
            return res.text()
        }).then(res => {
            console.log(res)
            if (res.code == 200) {
                console.log('200')
            }
        }).catch(e=>{
            console.log(e);
        })
    }
}

export {
    getCollection,
    addCollection,
    delCollection,
};